# 安装pymupdf  pip install pymupdf  -i https://pypi.tuna.tsinghua.edu.cn/simple

import fitz  # fitz就是PyMuPDF的别名

# 打开PDF文件
pdf_path = 'D:\Downloads\python语法\Pycharm使用快捷键.pdf'
pdf_document = fitz.open(pdf_path)

# 读取PDF文件的每一页
text = ""
lines = []
for page_num in range(len(pdf_document)):
    page = pdf_document.load_page(page_num)
    text += page.get_text()  # 获取页面文本内容
    lines.extend(text.split("\n"))

# 关闭PDF文件
pdf_document.close()


table = {}
table['title'] = lines[2]
table['column'] = [lines[3], lines[4], lines[5]]
table['rows'] = []

for i in range(6, 167, 3):
    startIndex = i
    endIndex = i + 3
    row = lines[startIndex:endIndex]
    tmp = (row[0], row[1], row[2])
    table['rows'].append(tmp)

outputFile = "res.txt"
with open(outputFile, 'w', encoding="utf-8") as of:
    for row in table['rows']:
        line = f'{str(row[0]).rjust(3, "0")} {str(row[1]).ljust(20, " ")} {str(row[2]).ljust(20, " ")}'
        print(line)
        of.write(line + "\n")
